﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class MP_FieldPositionDuties : System.Web.UI.Page
    {
        IFieldDutyBLL FieldDuty = new FieldDutyBLL();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //LoadRootNodes(RadTreeView1, TreeNodeExpandMode.ServerSideCallBack);
                //RadTreeNode selected_node = RadTreeView1.SelectedNode;
                //if (selected_node == null)
                //{
                //    DutyControl1.Visible = false;
                //    DutyDtlsControl1.Visible = false;
                //}
            }
        }

        private void BindTree(RadTreeView treeView)
        {
            IFieldDutyTreeBLL dutyTree = new FieldDutyTreeBLL();

            treeView.DataSource = dutyTree.GetFieldDutyTree();
            treeView.DataBind();
        }

        private static void LoadRootNodes(RadTreeView treeView, TreeNodeExpandMode expandMode)
        {
            IFieldDutyTreeBLL dutyTree = new FieldDutyTreeBLL();

            foreach (FieldDutyTreeInfo info in dutyTree.GetFieldDutyTree(""))
            {
                RadTreeNode node = new RadTreeNode();
                node.Text = info.Name;
                node.ToolTip = info.Descr;
                node.Value = info.ID;
                node.ImageUrl = info.ImageUrl;
                if (Convert.ToInt32(info.ChildrenCount) > 0)
                {
                    node.ExpandMode = expandMode;
                }
                treeView.Nodes.Add(node);
            }
        }

        private static void PopulateNodeOnDemand(RadTreeNodeEventArgs e, TreeNodeExpandMode expandMode)
        {
            e.Node.Dispose();
            e.Node.Nodes.Clear();
            IFieldDutyTreeBLL bll = new FieldDutyTreeBLL();
            foreach (FieldDutyTreeInfo info in bll.GetFieldDutyTree(e.Node.Value))
            {
                RadTreeNode node = new RadTreeNode();
                node.Text = info.Name;
                node.ToolTip = info.Descr;
                node.Value = info.ID;
                node.ImageUrl = info.ImageUrl;
                if (Convert.ToInt32(info.ChildrenCount) > 0)
                {
                    node.ExpandMode = expandMode;
                }
                e.Node.Nodes.Add(node);
            }

            e.Node.Expanded = true;
            e.Node.ExpandMode = expandMode;
        }

        public void PopulateNodeOnEditGrid(RadTreeNodeEventArgs e, TreeNodeExpandMode expandMode)
        {
            IFieldDutyTreeBLL bll = new FieldDutyTreeBLL();
            foreach (FieldDutyTreeInfo info in bll.GetFieldDutyTree(e.Node.Value))
            {
                RadTreeNode node = new RadTreeNode();
                node.Text = info.Name;
                node.ToolTip = info.Descr;
                node.Value = info.ID;
                node.ImageUrl = info.ImageUrl;
                if (Convert.ToInt32(info.ChildrenCount) > 0)
                {
                    node.ExpandMode = expandMode;
                }
                e.Node.Nodes.Add(node);
            }
            e.Node.Nodes.Clear();
            e.Node.ExpandMode = expandMode;
        }

        protected void btnExpand_Click(object sender, ImageClickEventArgs e)
        {
            IFieldDutyTreeBLL bll = new FieldDutyTreeBLL();
 
            //RadTreeView1.Nodes.Clear();
            //RadTreeView1.DataSource = bll.GetFieldDutyTree();
            //RadTreeView1.DataBind();
            //RadTreeView1.ExpandAllNodes();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            IFieldDutyTreeBLL bll = new FieldDutyTreeBLL();

            //RadTreeView1.Nodes.Clear();
            //if (txtSearch.Text.Length > 0)
            //{
            //    RadTreeView1.DataSource = bll.GetHashCode(txtSearch.Text);
            //    RadTreeView1.DataBind();
            //    RadTreeView1.ExpandAllNodes();
            //    lblResult.Text = RadTreeView1.GetAllNodes().Count.ToString();
            //    PanelSearch.Visible = true;
            //}
            //else
            //{
            //    LoadRootNodes(RadTreeView1, TreeNodeExpandMode.ServerSideCallBack);
            //    PanelSearch.Visible = false;
            //}
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            IFieldDutyTreeBLL bll = new FieldDutyTreeBLL();

            //RadTreeView1.Nodes.Clear();
            //LoadRootNodes(RadTreeView1, TreeNodeExpandMode.ServerSideCallBack);
            //PanelSearch.Visible = false;
        }

        protected void RadTreeView1_NodeClick(object sender, RadTreeNodeEventArgs e)
        {
            //RadTreeNode selected_node = RadTreeView1.SelectedNode;

            //string value = selected_node.Value;
            //string root_value = value.Substring(0, 1);
            //string node_value = value.Substring(2);

            //if (!string.IsNullOrEmpty(value))
            //{
            //    if (root_value == "1")
            //    {
            //        Session["FieldSeq"] = node_value;
            //        Session["FieldDutySeq"] = "";
            //        Session["FieldDutyDtlsSeq"] = "";
            //        DutyControl1.Visible = false;
            //        DutyDtlsControl1.Visible = false;
            //    }
            //    else if (root_value == "2")
            //    {
            //        Session["FieldDutySeq"] = node_value;
            //        Session["FieldDutyDtlsSeq"] = "";
            //        DutyControl1.Visible = true;
            //        DutyDtlsControl1.Visible = false;
            //    }
            //    else if (root_value == "3")
            //    {
            //        Session["FieldDutyDtlsSeq"] = node_value;
            //        DutyDtlsControl1.Visible = true;
            //        DutyControl1.Visible = false;
            //    }
            //    else 
            //    {
            //        DutyControl1.Visible = false;
            //        DutyDtlsControl1.Visible = false;
            //    }
            //}
            
        }

        protected void btnCollapse_Click(object sender, EventArgs e)
        {
            //RadTreeView1.CollapseAllNodes();
        }

        protected void RadManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            //if (e.Argument == "CloseWindow")
            //{
            //    if (DutyControl1.Visible)
            //    {
            //        DutyControl1.LoadFieldPositionData(Int32.Parse(Session["FieldSeq"].ToString()));
            //        DutyControl1.LoadFieldDutyData(1);
            //    }
            //    if (DutyDtlsControl1.Visible)
            //    {
                    
            //    }
 
            //    RefreshTree();
            //}
            //else
            //{
            //    if (DutyControl1.Visible)
            //    {
            //        DutyControl1.GetKeyValue(e.Argument);
            //    }
            //    if (DutyDtlsControl1.Visible)
            //    {
            //        //DutyDtlsControl1.GetKeyValue(e.Argument);
            //    }
            //}
        }

        public void RefreshTree()
        {
            //RadTreeNode selected_node = RadTreeView1.SelectedNode;
            //if (selected_node == null)
            //{
            //    selected_node = RadTreeView1.FindNodeByValue("0");
            //}
            //RadTreeNodeEventArgs node = new RadTreeNodeEventArgs(selected_node);
            //RadManager1.ResponseScripts.Add(@"CollapseSelectTree();");
            //PopulateNodeOnEditGrid(node, TreeNodeExpandMode.ServerSideCallBack);
            //RadManager1.ResponseScripts.Add(@"ExpandSelectTree();");
        }

        //protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        //{
        //    if (e.CommandName == "Delete")
        //    {
        //        int seq = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["field_duty_seq"]);
        //        FieldDuty.Delete(seq, 1);
        //    }

        //    RadGrid1.DataSource = FieldDuty.LoadGridFieldPositionDuties(1);
        //}

        protected void cmdSearch_Click(object sender, ImageClickEventArgs e)
        {
            IFieldDutyTreeBLL bll = new FieldDutyTreeBLL();

            //RadTreeView1.Nodes.Clear();
            //if (txtSearch.Text.Length > 0)
            //{
            //    RadTreeView1.DataSource = bll.GetResourceMasterResultSearch(txtSearch.Text);
            //    RadTreeView1.DataBind();
            //    RadTreeView1.ExpandAllNodes();
            //    lblResult.Text = RadTreeView1.GetAllNodes().Count.ToString();
            //    PanelSearch.Visible = true;
            //}
            //else
            //{
            //    LoadRootNodes(RadTreeView1, TreeNodeExpandMode.ServerSideCallBack);
            //    PanelSearch.Visible = false;
            //}
        }

        protected void cmdExpand_Click(object sender, ImageClickEventArgs e)
        {
            //I_HRM_MP_FieldDutyTree bll = new B_HRM_MP_FieldDutyTree();

            //RadTreeView1.Nodes.Clear();
            //RadTreeView1.DataSource = bll.GetFieldDutyTree();
            //RadTreeView1.DataBind();
            //RadTreeView1.ExpandAllNodes();
        }

        protected void cmdCollapse_Click(object sender, ImageClickEventArgs e)
        {
            //RadTreeView1.CollapseAllNodes();
        }

        protected void RadTreeView1_NodeExpand(object sender, RadTreeNodeEventArgs e)
        {
            PopulateNodeOnDemand(e, TreeNodeExpandMode.ServerSideCallBack);

        }
    }
}
